package org.example.chain.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.chain.service.impl.PhoneValidationChain;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/chain")
public class ChainController {

    private static final PhoneValidationChain phoneValidationChain = new PhoneValidationChain();

    /**
     * 使用责任链,做校验，@Validated底层原理
     *
     * @param phone : 手机号
     * @return
     */
    @GetMapping
    public String chain(@RequestParam String phone) {
        // 参数校验
        try {
            phoneValidationChain.execute(phone);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // 这里只是为了给前端输出明细的测试结果,简单演示，真实项目使用异常全局捕获返回
            return e.getMessage();
        }

        log.info("通过校验,执行业务");
        return "ok";
    }

}
